<?php
//if not a real page (nobody called us here, just user trying to hack
if ((!isset($_SESSION['realpage']))
 || ($_SESSION['realpage'] == false))
 {
 	require './hack.php';
 	die(" ");
 }

error_reporting(E_ALL);


if (0 > version_compare(PHP_VERSION, '5')) {
	die('This file was generated for PHP 5');
}
class datediff {
	public $y=0;
	public $m=0;
	public $d=0;
	public $h=0;
	public $i=0;
	public $s=0;	
}
class Message
{
	// --- ASSOCIATIONS ---


	// --- ATTRIBUTES ---


	public $mid = null;
	
	public $sender = null;
	
	public $senderName = null;
	
	public $receiver = null;
	
	public $receiverName = null;
	
	public $date = null;
	
	public $content = null;
	
	public $lostTime = null;
	
	public function short($length)
	{
		$explded = explode("<br",$this->content,3);
		
		if (strlen($explded[0]) <= $length)
			return strlen($explded[0]) == strlen($this->content) ?$this->content : $explded[0]."..";
		
		$ind=$length;
		while(($this->content[$ind] != ' ') && ($ind >0))
		{
			$ind--;
		}		
		if ($ind == 0)
			$ind = $length;
		$tmp=substr($this->content, 0,$length);
		$explded = explode("<br",$tmp,3);
					
		return $explded[0] . '..';
	}
	
	public function timePassed()
	{
		$nowDateTime = new DateTime("now");
		$interval = $this->get_date_diff($nowDateTime,new DateTime($this->date)) ;
		
		if ($interval->y > 0)
			return 	$interval->y . ' years';
			
		if ($interval->m > 0)
			return 	$interval->m . ' months';
			
		if ($interval->d > 0)
			return 	$interval->d . ' days';		
				
		if ($interval->h > 0)
			if ($interval->i > 30)
				return 	$interval->h . '.5 hours';
			else
				return 	$interval->h . ' hours';
				
		if ($interval->i > 0)
			return 	$interval->i . ' minutes';
				
		if ($interval->s < 20)
			return 	'a few seconds';
			
		return 	'less than a minute';
	}
	public static function factorMessage($sqlArr)
	{
		//$sql="SELECT t.thread AS 'thread', m.sender as 'sender',m.receiver as 'receiver',m.Created as 'date',m.content as 'message'
		
		$obj = new Message();     
		$flags = ENT_COMPAT | ENT_QUOTES ;

		$obj->mid       = $sqlArr['thread'];	  	  //meta (id)
		$obj->sender    = $sqlArr['sender'];	  	  //meta
		$obj->receiver  = $sqlArr['receiver'];	  	  //meta
		$obj->date  	= $sqlArr['date'];	  		  //meta	
		
		$obj->content		 = htmlspecialchars ( $sqlArr['message'],$flags ,'UTF-8');
		$obj->receiverName	 = htmlspecialchars ( $sqlArr['receiverName'],$flags ,'UTF-8');
		$obj->senderName	 = htmlspecialchars ( $sqlArr['senderName'],$flags ,'UTF-8');
		$obj->lostTime 		 = $obj->timePassed();

		$cnt = 70;
		$obj->content = htmlspecialchars ( $sqlArr['message'],$flags ,'UTF-8');
		$obj->content = str_replace("[dropline]", "<br>",$obj->content,$cnt );
		
		$obj->thread = $obj->short(270);
		
		return  $obj;	
	}

	
public function get_date_diff($newer, $older) {
  $Y1 = $older->format('Y');
  $Y2 = $newer->format('Y');
  $Y = $Y2 - $Y1;

  $m1 = $older->format('m');
  $m2 = $newer->format('m');
  $m = $m2 - $m1;

  $d1 = $older->format('d');
  $d2 = $newer->format('d');
  $d = $d2 - $d1;

  $H1 = $older->format('H');
  $H2 = $newer->format('H');
  $H = $H2 - $H1;

  $i1 = $older->format('i');
  $i2 = $newer->format('i');
  $i = $i2 - $i1;

  $s1 = $older->format('s');
  $s2 = $newer->format('s');
  $s = $s2 - $s1;

  $diffestamp = new datediff();
  $diffestamp->y = $Y;
  $diffestamp->m = $m;
  $diffestamp->d = $d;
  $diffestamp->h = $H;
  $diffestamp->i = $i;
  $diffestamp->s = $s;
  
  return $diffestamp;
}

} /* end of class Message */

?>